package com.blrs.service.project.costcontrol.impl;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import com.blrs.common.utils.SecurityUtils;
import com.blrs.common.utils.uuid.IdUtils;
import com.blrs.domain.CostControl;
import com.blrs.domain.ProjectInfo;
import com.blrs.domain.vo.BusinessMainClassVo;
import com.blrs.mapper.CostControlMapper;
import com.blrs.service.project.costcontrol.ICostControlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 成本管理Service业务层处理
 * 
 * @author yuxiang.chen
 * @date 2023-08-03
 */
@Service
public class CostControlServiceImpl implements ICostControlService
{
    @Autowired
    private CostControlMapper costControlMapper;


    /**
     * 查询成本管理
     * 
     * @param id 成本管理主键
     * @return 成本管理
     */
    @Override
    public CostControl selectCostControlById(String id)
    {
        return costControlMapper.selectCostControlById(id);
    }

    /**
     * 查询成本管理列表
     * 
     * @param costControl 成本管理
     * @return 成本管理
     */
    @Override
    public List<CostControl> selectCostControlList(CostControl costControl)
    {
        Long userid = SecurityUtils.getUserId();
        if(userid!=1){
            costControl.setBelongTo(SecurityUtils.getOrgId().toString());
        }
        return costControlMapper.selectCostControlList(costControl);
    }
    //修改主分类
    @Override
    public int editMainClass(BusinessMainClassVo businessMainClassVo) {
        boolean flag = businessMainClassVo.getBusinessId().contains(",");
        int count = 0;
        if (flag) {
            String[] businessIds = businessMainClassVo.getBusinessId().split(",");
            for (int i = 0; i < businessIds.length; i++) {
                CostControl info = costControlMapper.selectCostControlById(businessIds[i]);
                info.setClassificationId(businessMainClassVo.getMainClassId());
                info.setOperateTime(new Timestamp(new Date().getTime()));
                info.setOperateName(SecurityUtils.getUsername());
                info.setOperateId(SecurityUtils.getUserId());
                count = costControlMapper.updateCostControl(info);
            }
        } else {
            CostControl info = costControlMapper.selectCostControlById(businessMainClassVo.getBusinessId());
            info.setClassificationId(businessMainClassVo.getMainClassId());
            info.setOperateTime(new Timestamp(new Date().getTime()));
            info.setOperateName(SecurityUtils.getUsername());
            info.setOperateId(SecurityUtils.getUserId());
            count = costControlMapper.updateCostControl(info);
        }
        return count;
    }
    /**
     * 新增成本管理
     * 
     * @param costControl 成本管理
     * @return 结果
     */
    @Override
    public int insertCostControl(CostControl costControl)
    {
        costControl.setDelFlag("0");
        costControl.setStatus("0");
        costControl.setSource("1");//人工添加
        costControl.setId(IdUtils.fastUUID());
        costControl.setDepartmentId(SecurityUtils.getDeptId());
        costControl.setDepartmentName(SecurityUtils.getLoginUser().getUser().getDept().getDeptName());
        costControl.setOrganizationId(SecurityUtils.getOrgId());
        costControl.setOrganizationName(SecurityUtils.getOrgName());
        costControl.setBelongTo(SecurityUtils.getOrgId().toString());
        costControl.setGenerateTime(new Timestamp(new Date().getTime()));
        return costControlMapper.insertCostControl(costControl);
    }

    /**
     * 修改成本管理
     * 
     * @param costControl 成本管理
     * @return 结果
     */
    @Override
    public int updateCostControl(CostControl costControl)
    {
        //执行确认操作
        if(costControl.getStatus().equals("1")){
            costControl.setOperateId(SecurityUtils.getUserId());
            costControl.setOperateName(SecurityUtils.getLoginUser().getUser().getNickName());
            costControl.setOperateTime(new Timestamp(new Date().getTime()));
        }

        return costControlMapper.updateCostControl(costControl);
    }

    /**
     * 批量删除成本管理
     * 
     * @param ids 需要删除的成本管理主键
     * @return 结果
     */
    @Override
    public int deleteCostControlByIds(String[] ids)
    {
        return costControlMapper.deleteCostControlByIds(ids);
    }

    /**
     * 删除成本管理信息
     * 
     * @param id 成本管理主键
     * @return 结果
     */
    @Override
    public int deleteCostControlById(String id)
    {
        return costControlMapper.deleteCostControlById(id);
    }
}
